unit plLst;

interface
uses
  Classes, tags;
procedure Playlist_Add_FromFld(const f_path: widestring);
procedure PlayList_add(const filename: widestring);
procedure PlayList_Clear;
procedure PlayList_save;
procedure PlayList_deleteselected;
procedure PlayList_delete(const id: Cardinal);
procedure PlayList_Load_From_Playlist(const file_name: widestring);
procedure PlayList_Load_FromStringLst(var lst: TstringList);

var
  pl: array[0..255] of PL_Tags;
  fl: array[0..255] of TF_INFO;
  l_size: longint;
implementation
uses Unit1, utils, Pl_Consts, SysUtils, trans_ru, Windows;

procedure Playlist_Add_FromFld(const f_path: widestring);
var
  list: TstringList;
begin
  list := Get_Files(f_path, '*.*')^;
  PlayList_Load_FromStringLst(list);
end;

procedure PlayList_Save;
begin
end;

procedure PlayList_Clear;
begin
  with Unit1.PlMain do
  begin
    l_size := 0;
    playlist.Clear;
  end;
end;

procedure PlayList_deleteselected;
var
  i: longint;
begin
  with Unit1.PlMain do
  begin
    if (playlist.Count  > 0) then
    begin
      i := playlist.Count - 1;
      while (i >= 0) do
      begin
        if (playlist.selected[i]) then PlayList_delete(i);
        dec(i);
      end;
    end;
  end;
end;

procedure PlayList_delete(const id: Cardinal);
var
  i: Longint;
begin
    if (l_size = 0) then exit;
    for i := id + 1 to l_size - 1 do
    begin
      pl[i - 1] := pl[i];
      fl[i - 1] := fl[i];
    end;
    Unit1.PlMain.playlist.Selected[id] := false;
    Unit1.PlMain.playlist.Items.Delete(id);
    dec(l_size);
end;

procedure PlayList_add(const filename: widestring);
var
    info: Ptf_info;
    str: widestring;
    _tab: widestring;
    procedure get_tab;
    var
      i: longint;
    begin
      _tab := '';
      for i :=1 to 8 do _tab := _tab + ' ';
    end;
begin
  get_tab;
  info := Pl_Add(filename);
  if (info = nil)  then exit;
  inc(l_size);
  fl[l_size - 1] := info^;
  if (fl[l_size - 1].file_path <> filename) then
     fl[l_size - 1].file_path := pWidechar(filename);
  if (fl[l_size - 1].tags <> nil) then
    pl[l_size - 1] := Unname_Tags(fl[l_size - 1].tags, fl[l_size - 1].filetype);
  str := extractFileName(filename);
  if pl[l_size - 1].title <> nil then
    str := str + _tab + pl[l_size - 1].title;
  unit1.PlMain.playlist.Items.Append(str);
end;

procedure PlayList_Load_FromStringLst(var lst: TstringList);
var
  i: Cardinal;
begin
  if ((lst = nil) or (lst.Count = 0)) then exit;
  for i := 0 to lst.Count - 1 do
    PlayList_add(lst.Strings[i]);
  lst.FreeInstance;
end;

procedure PlayList_Load_From_Playlist(const file_name: widestring);
var
  lst: TstringList;
  i: cardinal;
  str: string;
begin
  with unit1.PlMain do
  begin
    playlist.Clear;
    lst := TstringList.Create;
    if (not FileExists(file_name)) then
    begin
      _error(_TEFILE_NOT_SELECTED + path.Text);
      exit;
    end;
    lst.LoadFromFile(file_name);
    PlayList_Load_FromStringLst(lst);
  end;
end;

end.